package com.zhongxi.guoyuan.widget.view;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.utils.UiUtils;

/**
 * Created by Administrator on 2018/1/9.
 */

public abstract class LoadPager extends FrameLayout {

    public static final int STATUS_UNKNOW = 1091;    //未知状态
    public static final int STATUS_LOAGING = 1092;   //加载中...

    //联网请求后的状态
    public static final int STATUS_ERROR = 1093;     //加载失败
    public static final int STATUS_EMPTY = 1094;      //加载为空
    public static final int STATUS_SUCCESS = 1095;    //加载成功

    View lodingView; //加载中..
    View errorView;//加载失败
    View emptyView; //加载为空
    View successView; //加载成功

    //  TODO 不能写成静态(共享)  默认状态
    private int status = STATUS_LOAGING;

    public LoadPager(@NonNull Context context) {
        super(context);
        init();
    }

    public LoadPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LoadPager(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    /*将四种布局添加到帧布局中*/
    private void init() {
        if (lodingView == null) {
            lodingView = getLoadingView();
            addView(lodingView, new FrameLayout.LayoutParams(-1, -1));
        }
        if (errorView == null) {
            errorView = getErrorView();
            addView(errorView, new FrameLayout.LayoutParams(-1, -1));
        }
        if (emptyView == null) {
            emptyView = getEmptyView();
            addView(emptyView, new FrameLayout.LayoutParams(-1, -1));
        }
        showPage();
    }

    /*根据状态切换不同的页面*/
    private void showPage() {
        if (lodingView != null) {
            lodingView.setVisibility(status == STATUS_UNKNOW || status == STATUS_LOAGING ? View.VISIBLE : View.INVISIBLE);
        }
        if (errorView != null) {
            errorView.setVisibility(status == STATUS_ERROR ? View.VISIBLE : View.INVISIBLE);
        }
        if (emptyView != null) {
            emptyView.setVisibility(status == STATUS_EMPTY ? View.VISIBLE : View.INVISIBLE);
        }
        //判断成功状态来创建成功页面
        if (status == STATUS_SUCCESS) {
            if (successView == null) {
                successView = getSuccessView();
                addView(successView, new FrameLayout.LayoutParams(-1, -1));
            }
        }
    }

    /*记载中...*/
    private View getLoadingView() {
        return UiUtils.inflate(R.layout.loadingview_layout);
    }

    /*加载失败*/
    private View getErrorView() {
        View view = UiUtils.inflate(R.layout.errorview_layout);
        view.findViewById(R.id.page_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show();  //重新请求服务器
            }
        });
        return view;
    }

    /*加载为空*/
    private View getEmptyView() {
        return UiUtils.inflate(R.layout.emptyview_layout);
    }

    /*根据联网状态显示不同的界面*/
    public void show() {
        if (status == STATUS_UNKNOW || status == STATUS_ERROR) {
            status = STATUS_LOAGING;
        }
        showPage();

        status = load();  //lianwang

        showPage();

    }

    /**
     * 请求服务器数据,返回对应的状态<br>
     * {@link #STATUS_ERROR 加载失败} <br>
     * {@link #STATUS_EMPTY 加载为空} <br>
     * {@link #STATUS_SUCCESS 加载成功} <br>
     *
     * @return
     */
    public abstract int load();

    /**
     * 创建成功<br>
     * 只有{@link #load()} 的返回值是{@link #STATUS_SUCCESS}时,才会执行
     *
     * @return
     */
    protected abstract View getSuccessView();

}
